# -*- coding: utf-8 -*-
"""
@Author：mengying
@file： spider.py
@date：2024/2/28 18:23
@email: 652044581@qq.com
@desc: 
"""
import os
import random

# -*- coding: utf-8 -*-
"""
@Author：mengying
@file： jiangong-spider.py
@date：2024/2/28 18:10
@email: 652044581@qq.com
@desc: 
"""
import platform
from playwright.sync_api import sync_playwright, Playwright
import xlrd
import time
import requests
from datetime import datetime

excel_path = r"C:\Users\Administrator\Desktop\山东 1uuid(1).xlsx"
container_path = r"C:\gongjian"
wk = xlrd.open_workbook(excel_path)
wb = wk.sheet_by_index(0)

session = requests.session()


class DriverWorker:
    """playwright驱动类"""

    def __init__(self, playwright: Playwright):
        headless = True
        if 'windows' in platform.platform().lower():
            headless = False
        self.browser = playwright.chromium.launch(headless=headless, args=['--start-maximized'])
        js = """Object.defineProperties(navigator, {webdriver:{get:()=>undefined}});"""
        self.context = self.browser.new_context(no_viewport=True)
        self.page = self.context.new_page()
        self.page.add_init_script(js)

    def run(self, url: str) -> None:
        self.page.goto(url, wait_until="load", timeout=10000)

    def get_content(self):
        return self.page.content()

    def button_moved(self, xpath):
        self.page.query_selector(xpath).hover()
        location = self.page.locator(xpath).bounding_box()
        self.page.mouse.down()
        self.page.mouse.move(x=int(location['x'] + 500), y=int(location['x']), steps=100)
        self.page.mouse.up()

    def get_cookies(self):
        cookie_list = self.context.cookies()
        cookie_dict = {}
        for cookie in cookie_list:
            cookie_dict[cookie['name']] = cookie['value']
        return cookie_dict

    def get_uri(self):
        return self.page.url

    @classmethod
    def read_excel(cls):
        nrows = wb.nrows
        for i in range(nrows):
            uid = wb.cell_value(i, 1)
            filePath = os.path.join(container_path, uid + ".txt")
            if os.path.exists(filePath):
                continue
            else:
                print("time: %s uid: %s 处理中-----------------" % (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), uid))
                return uid


def main():
    with sync_playwright() as playwright:
        driver = DriverWorker(playwright)
        driver.run("https://businessclient.jiangongdata.com/")
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
        }

        # 输入账号密码
        driver.page.fill("[placeholder=\"请输入手机号\"]", "18754181000")
        driver.page.fill("[placeholder=\"请输入密码\"]", "houyanxue123")
        driver.button_moved("#nc_1_n1z")

        driver.page.click("button:has-text('登录')", )
        time.sleep(5)

        print(driver.page.url)
        if driver.page.url != "https://businessclient.jiangongdata.com/":
            return

        cookie = driver.get_cookies()
        session.cookies.update(cookie)

        while True:
            uid = DriverWorker.read_excel()
            filePath = os.path.join(container_path, uid + ".txt")
            uri = 'https://businessclient.jiangongdata.com/renyuan/{}.html'.format(uid)
            try:
                resp = session.get(uri, headers=headers, timeout=10)
            except Exception as e:
                continue
            else:
                if resp.status_code != 200:
                    return

                if "您好，欢迎登录" in resp.text:
                    return

                with open(filePath, "wt", encoding="utf-8") as f:
                    f.write(resp.text)
                time.sleep(random.Random().randint(1, 3))


if __name__ == '__main__':
    while True:
        try:
            main()
        except Exception as e:
            time.sleep(10)
            continue
